app.controller('CouponCodeList', function($scope, $http, common){
	$scope.list 	= new Array();
	$scope.pageHtml = '';
	$scope.nPage 	= 1;
	$scope.aCode 	= {};
	$scope.total 	= {};

	//初始化JS
	$scope.init_js 	= function(){
		$('.datepicker').datetimepicker({
	        language:  'zh-CN',
	        weekStart: 1,
	        todayBtn:  1,
	        autoclose: 1,
	        todayHighlight: 1,
	        startView: 2,
	        forceParse: 0,
	        showMeridian: 1,
	        minView	: 1
	    });
	}

	//加载数据
	$scope.load_data 	= function(page){
		var url 	= config.prefix_url+'/Coupon/getCodeList?p='+page;
		$http.get(url).success(function(info){
			$scope.load_style('stop');
			if(info.code != 1){
				common.dealError(info);
			}else{
				$scope.nPage 	= page;
				$scope.list 	= info.data.list;
				$scope.pageHtml = info.data.page_html;
				common.startPage($scope.load_data);
			}
		});

		$scope.load_style('start');
	}

	//更改状态
	$scope.change_status 	= function(codeId, status){
		var url 	= config.prefix_url+'/Coupon/changeCodeStatus';
		var param 	= {
			'code_id'	: codeId,
			'status'	: status
		}

		$http.post(url, param).success(function(info){
			$scope.load_style('stop');
			if(info.code != 1){
				common.dealError(info);
			}else{
				console.log(info);
				$scope.load_data($scope.nPage);
			}
		})
		$scope.load_style('start');
	}

	//生成邀请码
	$scope.create_code 	= function(){
		var url 	= config.prefix_url+'/Coupon/createCouponCode';
		var param 	= $scope.aCode;
		$http.post(url, param).success(function(info){
			$scope.load_style('stop');
			$('.modal.code-add').modal('hide');
			if(info.code != 1){
				common.dealError(info);
			}else{
				$scope.load_data($scope.nPage);
				$scope.aCode 	= {};
			}
		});
		$scope.load_style('start');
	}

	//‘加载’样式
    $scope.load_style   = function(type){
        switch(type){
            case 'start':
                $('.title').children('.fa-spinner').css('display', 'inline');
            break;
            case 'stop':
                $('.title').children('.fa-spinner').css('display', 'none');
            break;
        }
    }

	$scope.load_data(1);
	$scope.init_js();
});

app.controller('CouponUseLog', function($scope, $http, common){
	$scope.list 	= '';
	$scope.dateS 	= '';
	$scope.dateE 	= '';
	$scope.nPage 	= 1;
	$scope.pageHtml = '';
	$scope.loading 	= false;

	//初始化JS
	$scope.init_js 	= function(){
		$('.input-date').datetimepicker({
	        language:  'zh-CN',
	        format: 'yyyy-mm-dd',
	        weekStart: 1,
	        todayBtn:  1,
	        autoclose: 1,
	        todayHighlight: 1,
	        startView: 2,
	        forceParse: 0,
	        showMeridian: 1,
	        minView	: 2
	    });
	}

	//加载数据
	$scope.load_data 	= function(page){
		var page 	= page ? page : 1;
		var url 	= config.prefix_url+'/Coupon/getUseLog?'+
			'&date_start='+$scope.dateS+
			'&date_end='+$scope.dateE+
			'&page='+page;

		$http.get(url).success(function(info){
			$scope.loading 	= false;
			if(info.code == 0){
				common.dealError(info);
				return false;
			}
			$scope.nPage 	= page;
			$scope.list 	= info.data.list;
			$scope.pageHtml = info.data.page_html;
			$scope.total 	= info.data.total;
			common.startPage($scope.load_data);
		})
		$scope.loading 	= true;
	}

	//导出excel
	$scope.out_excel 	= function(){
		var url 	= config.prefix_url + '/Coupon/outExcelUseLog?'+
			'&date_start='+$scope.dateS+
			'&date_end='+$scope.dateE;
		$http.get(url).success(function(info){
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			var count 	= info.data.count;
			var start 	= 0;
			var length 	= 5000;
			while(start < count){
				var _url 	= url + '&start='+start+'&length='+length;
				start 	+= length;
				// console.log(_url);
				window.open(_url, '_blank');
			}
		})
	}

	$scope.init_js();
	$scope.load_data(1);
})